package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.utils.MD5Util;
import com.itheima.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;

public class UserServiceImpl implements UserService {
    @Override
    public PageInfo<User> findByPage(int currentPage, int pageSize) {
        PageInfo<User> info = null;
        SqlSession sqlSession=null;
        try {
            //1 获取SqlSession对象
            sqlSession= MybatisUtil.getSqlSession();
            //2 获取dao对应的代理对象
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
            //3 执行操作
            //3.1 设置分页参数
            PageHelper.startPage(currentPage,pageSize);
            //3.2 查询所有
            List<User> list = userDao.findAll();
            //3.3 封装分页结果
            info = new PageInfo<>(list);
            //4 返回结果并释放资源
            MybatisUtil.commit(sqlSession);
        }finally {
            //释放资源
            MybatisUtil.close(sqlSession);
        }
        return info;
    }

    //保存信息
    @Override
    public void save(User user) {
        SqlSession sqlSession=null;
        try {
            //1 获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2 获取dao对应的代理对象
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
            //给密码加密
            String password = MD5Util.md5(user.getPassword());
            user.setPassword(password);
            //生成唯一id
            String uuid = UUID.randomUUID().toString().replace("-", "");
            user.setId(uuid);
            //3 执行操作
            userDao.save(user);
            //4 提交事务
            MybatisUtil.commit(sqlSession);
        } finally {
            //4 释放资源
            MybatisUtil.close(sqlSession);
        }
    }

    //根据id查询信息的方法
    @Override
    public User findById(String id) {
        User user = null;
        SqlSession sqlSession=null;
        try {
            //1 获取SqlSession对象
            sqlSession= MybatisUtil.getSqlSession();
            //2 获取dao对应的代理对象
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
            //3 执行操作
            //3.2 查询所有
            user = userDao.findById(id);
            //4 返回结果并释放资源
            MybatisUtil.commit(sqlSession);
        }finally {
            //释放资源
            MybatisUtil.close(sqlSession);
        }
        return user;
    }

    //修改数据的方法
    @Override
    public void update(User user) {
        SqlSession sqlSession=null;
        try {
            //1 获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2 获取dao对应的代理对象
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
            //3 执行操作
            userDao.update(user);
            //4 提交事务
            MybatisUtil.commit(sqlSession);
        } finally {
            //4 释放资源
            MybatisUtil.close(sqlSession);
        }
    }

    //批量删除信息的方法
    @Override
    public void delete(String[] ids) {
        SqlSession sqlSession=null;
        try {
            //1 获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2 获取dao对应的代理对象
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
            //3 执行操作
            Stream.of(ids).forEach(id->{
                //删除用户和角色之间的对应关系
                userDao.deleteRoleAssociationByUserId(id);
                //删除用户信息
                userDao.delete(id);
            });
            //4 提交事务
            MybatisUtil.commit(sqlSession);
        } finally {
            //4 释放资源
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public List<User> findAll() {
        List<User> list = null;
        SqlSession sqlSession=null;
        try {
            //1 获取SqlSession对象
            sqlSession= MybatisUtil.getSqlSession();
            //2 获取dao对应的代理对象
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
            //3 执行操作
            //3.2 查询所有
            list = userDao.findAll();
            //4 返回结果并释放资源
            MybatisUtil.commit(sqlSession);
        }finally {
            //释放资源
            MybatisUtil.close(sqlSession);
        }
        return list;
    }

    //根据用户绑定的角色也叫给用户授权
    @Override
    public void updateUserAndRoles(String userId, String[] checkedRoleIds) {
        SqlSession sqlSession=null;
        try {
            //1 获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2 获取dao对应的代理对象
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
            //3 执行操作
            //3.1 根据用户id删除绑定的角色，操作ss_role_user
            userDao.deleteUserAndRoles(userId);
            //3.2 遍历checkedRoleIds，保存用户绑定的角色，操作ss_role_user
            Stream.of(checkedRoleIds).forEach(roleId->{
                userDao.saveUserAndRoles(userId,roleId);
            });
            //4 提交事务
            MybatisUtil.commit(sqlSession);
        } finally {
            //4 释放资源
            MybatisUtil.close(sqlSession);
        }
    }

    //用户登录
    /*@Override
    public User login(String email, String password) {
        User user = null;
        SqlSession sqlSession=null;
        try {
            //1 获取SqlSession对象
            sqlSession= MybatisUtil.getSqlSession();
            //2 获取dao对应的代理对象
            UserDao userDao = MybatisUtil.getMapper(sqlSession, UserDao.class);
            //对密码进行加密
            password= MD5Util.md5(password);
            //3 执行操作
            user = userDao.findUserByEmailAndPassword(email,password);
            //4 返回结果并释放资源
            MybatisUtil.commit(sqlSession);
        }finally {
            //释放资源
            MybatisUtil.close(sqlSession);
        }
        return user;
    }*/
    //用户登录
    public User login(String email, String password) {
        SqlSession sqlSession = null;
        User user = null;
        try {
            //1.获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2.获取dao对应的代理对象
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            //3.执行操作
            password=MD5Util.md5(password);
            user = userDao.findUserByEmailAndPassword(email, password);
        } finally {
            //4.返回结果并释放资源
            MybatisUtil.close(sqlSession);
        }
        return user;
    }
}
